package com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.service;

import com.hymjweb.demo.SpringAopUtil;
import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.PinyinUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.add_cost_detail.bean.XtAirlinesAddCostDetailPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.add_cost_detail.service.XtAirlinesAddCostDetailService;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.bean.XtAirlinesCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.bean.XtAirlinesPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.mapper.XtAirlinesMapper;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class XtAirlinesService extends BaseService {
    @Autowired
    private XtAirlinesMapper xtAirlinesMapper;

    @Autowired
    private GlobalCacheService globalCacheService;

    @Autowired
    private XtAirlinesAddCostDetailService xtAirlinesAddCostDetailService;

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = xtAirlinesMapper.findAll(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    @Transactional(rollbackFor = {Exception.class})
    public int insertInit(XtAirlinesPojo pojo) throws Exception{
        if(pojo == null){
            throwException("保存航空公司参数错误");
        }
        checkPojo(pojo);
        //检查ID重复
        XtAirlinesPojo dbXtAirlinesPojoFindObjectById = fetch(pojo.getId());
        if(dbXtAirlinesPojoFindObjectById != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.exist_id.getName());
        }
        //检查UK
        XtAirlinesPojo dbXtAirlinesPojo = xtAirlinesMapper.findUK(pojo.getName(),null);
        if(dbXtAirlinesPojo != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.exist_name.getName());
        }

        Date currentTime = this.getSysTime();
        String czyId = this.getLoginCzyId();

        pojo.setCamelCharPinyin(PinyinUtil.getPinYinFirst(pojo.getFullPinyin()));
        pojo.setCreateTime(currentTime);
        pojo.setCreateUserId(czyId);
        pojo.setUpdateTime(currentTime);
        pojo.setUpdateUserId(czyId);

        int affectRows = SpringAopUtil.getBean(this.getClass()).insert(pojo);

        if(affectRows > SysConstant.INT_ZERO){
            globalCacheService.loadXtAirlinesCache();
        }
        return affectRows;
    }

    @Transactional(rollbackFor = {Exception.class})
    public int updateInit(XtAirlinesPojo pojo) throws Exception{
        if(pojo == null){
            throwException("更新航空公司参数错误");
        }
        checkPojo(pojo);
        //检查UK
        XtAirlinesPojo dbXtAirlinesPojo = xtAirlinesMapper.findUK(pojo.getName(),pojo.getId());
        if(dbXtAirlinesPojo != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.exist_name.getName());
        }
        pojo.setCamelCharPinyin(PinyinUtil.getPinYinFirst(pojo.getFullPinyin()));
        pojo.setUpdateTime(this.getSysTime());
        pojo.setUpdateUserId(this.getLoginCzyId());

        int affectRows = SpringAopUtil.getBean(this.getClass()).update(pojo);
        if(affectRows > SysConstant.INT_ZERO){
            globalCacheService.loadXtAirlinesCache();
        }
        return affectRows;
    }

    /**
     * 检查必输项
     * @param pojo
     */
    private void checkPojo(XtAirlinesPojo pojo) throws Exception{
        DataTypeUtil.isEmptyStrThenThrow(pojo.getId(), SysConstant.XT_DIC_ERROR_MSG.no_id.getName());
        DataTypeUtil.isEmptyStrThenThrow(pojo.getName(), SysConstant.XT_DIC_ERROR_MSG.no_name.getName());
    }

    public List<XtAirlinesCachePojo> findCacheAll(Map map){
        List<XtAirlinesCachePojo> pojos = xtAirlinesMapper.findCacheAll(map);
        return pojos;
    }

    @Transactional(rollbackFor = {Exception.class})
    public int deleteInit(String id) throws Exception{
        if(DataTypeUtil.isEmptyStr(id)){
            throwException(SysConstant.XT_DIC_ERROR_MSG.no_id.getName());
        }
        List<XtAirlinesAddCostDetailPojo> pojoList = xtAirlinesAddCostDetailService.findByAirlinesId(id);
        if(pojoList != null && pojoList.size() != 0){
            throwException(SysConstant.XT_DIC_ERROR_MSG.exist_child_node.getName());
        }
        int affectRows =  SpringAopUtil.getBean(this.getClass()).delete(id);

        if(affectRows > SysConstant.INT_ZERO){
            globalCacheService.loadXtAirlinesCache();
        }
        return affectRows;
    }


    public XtAirlinesPojo fetch(String id){
        return xtAirlinesMapper.fetch(id);
    }


    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle="航空公司-新增" , operMemo = "", classType=XtAirlinesPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(XtAirlinesPojo pojo) throws Exception {
        if(pojo == null){
            throwException("保存航空公司参数错误!");
        }
        return xtAirlinesMapper.insert(pojo);
    }

    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle="航空公司-修改" , operMemo = "", classType=XtAirlinesPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int  update(XtAirlinesPojo pojo) throws Exception {
        if(pojo == null){
            throwException("更新航空公司参数错误!");
        }
        return xtAirlinesMapper.update(pojo);
    }


    @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle="航空公司-删除" , operMemo = "", classType=String.class)
    @Transactional(rollbackFor = {Exception.class})
    public int  delete(String id) throws Exception {
        if(id == null){
            throwException("删除航空公司参数错误!");
        }
        return xtAirlinesMapper.delete(id);
    }



}
